// pages/set-balance-deficiency-hint/set-balance-deficiency-hint.js
import user from '../../utils/user'
import {
    getSystemSetUp,
    setPayRepeatTip,
    setSignInMode,
    setNeedAuthSalaryCard,
    setBalanceRemind,
    setFlagUserRemark
} from '../../api/app'
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        everyinsurancePrice: '',
        everyinsuranceRemind: 0,
        isBalanceRemind: false,
        moneyArr: ['10000', '50000', '100000', '自定义'],
        isBalanceRemindPopup: false,
        balancePrice: 0, // 弹出输入的价格
        isFlagUserRemark: false,
        isFlagQuicklyGetSalary: false,
        isCollectEmployeeBankCards: false,
        iseveryinsuranceopen: false,
        iseveryinsurancePopup: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    async onLoad(options) {
        await user.reLogin()
        user.waitLogin().then(() => {
            // 获取当前企业的设置
            getSystemSetUp().then(res => {
                console.log('resfasf', res, user)
                this.setData({
                    isOpen: res.isOpenPayRepeatReminder,
                    payrollCard: res.isNeedAuthSalaryCard,
                    signInMode: res.signInMode,
                    balanceRemind: user.balanceRemind,
                    isBalanceRemind: !!user.balanceRemind,
                    isFlagUserRemark: !!res.flagUserRemark,
                    isFlagQuicklyGetSalary: !!res.flagQuicklyGetSalary,
                    isCollectEmployeeBankCards: !!res.isCollectEmployeeBankCards,
                    everyinsuranceRemind: res.wesureReminderBalance,
                    iseveryinsuranceopen: res.isWesureInsufficientBalanceReminder === 1 ? true : false
                })
            })
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },
    // 开启日日保余额不足提醒
    switcheveryinsurance(e) {
        console.log(e)
        const {
            value
        } = e.detail
        if (value) {
            wx.showActionSheet({
                itemList: this.data.moneyArr,
                success: (res) => {
                    // 点击 自定义之外的金额 进入
                    if (res.tapIndex !== 3) {
                        setFlagUserRemark({
                            wesureReminderBalance: this.data.moneyArr[res.tapIndex],
                            isWesureInsufficientBalanceReminder: 1
                        }).then(() => {
                            this.setData({
                                iseveryinsuranceopen: true,
                                everyinsuranceRemind: this.data.moneyArr[res.tapIndex]
                            })
                            app.toast('修改成功', 'success')
                        })
                    } else {
                        // 点击自定义 进入
                        this.setData({
                            iseveryinsurancePopup: true
                        })
                    }
                },
                fail: (res) => {
                    console.log(res.errMsg)
                    if (!this.data.balanceRemind) {
                        this.setData({
                            everyinsuranceRemind: 0,
                            iseveryinsuranceopen: false
                        })
                    }
                }
            })
        } else {
            setFlagUserRemark({
                wesureReminderBalance: 0,
                isWesureInsufficientBalanceReminder: 0
            }).then(() => {
                this.setData({
                    iseveryinsuranceopen: false,
                    everyinsuranceRemind: 0
                })
                app.toast('关闭成功', 'success')
            })
        }
    },
    // 开启发薪专户余额不足提醒
    switchpaywage(e) {
        console.log(e)
        const {
            value
        } = e.detail
        if (value) {
            wx.showActionSheet({
                itemList: this.data.moneyArr,
                success: (res) => {
                    console.log(res.tapIndex)
                    // 点击 自定义之外的金额 进入
                    if (res.tapIndex !== 3) {
                        setBalanceRemind({
                            balanceRemind: this.data.moneyArr[res.tapIndex]
                        }).then(() => {
                            this.setData({
                                isBalanceRemind: true,
                                balanceRemind: this.data.moneyArr[res.tapIndex]
                            })
                            app.toast('修改成功', 'success')
                        })
                    } else {
                        // 点击自定义 进入
                        this.setData({
                            isBalanceRemindPopup: true
                        })
                    }
                },
                fail: (res) => {
                    console.log(res.errMsg)
                    if (!this.data.balanceRemind) {
                        this.setData({
                            balanceRemind: 0,
                            isBalanceRemind: false
                        })
                    }
                }
            })
        } else {
            setBalanceRemind({
                balanceRemind: 0
            }).then(res => {
                this.setData({
                    balanceRemind: 0,
                    isBalanceRemind: false
                })
                app.toast('关闭成功', 'success')
            })
        }
    },
    // 快捷修改余额不足提示 金额
    onQuickSetBalancePrice() {
        this.setData({
            isBalanceRemindPopup: true,
            balancePrice: this.data.balanceRemind // 做金额回显
        })
    },
    // 修改日日保提醒金额
    changeeveryinsurancenumber() {
        this.setData({
            iseveryinsurancePopup: true,
            everyinsurancePrice: this.data.everyinsuranceRemind // 做金额回显
        })
    },
    // 修改发薪专户提醒余额
    changepaywagenumber() {
        this.setData({
            isBalanceRemindPopup: true,
            balancePrice: this.data.balanceRemind // 做金额回显
        })
    },
    cancel() {
        this.setData({
            isBalanceRemindPopup: false,
        })
        // 检测他是否有设置 用过没有设置则 关闭
        if (!this.data.balanceRemind) {
            this.setData({
                isBalanceRemind: false
            })
        }
    },
    everyinsurancecancel() {
        this.setData({
            iseveryinsurancePopup: false,
        })
        // 检测他是否有设置 用过没有设置则 关闭
        if (this.data.everyinsuranceRemind === 0) {
            this.setData({
                iseveryinsuranceopen: false
            })
        }
    },
    // 余额不足提示弹窗价格设置
    setBalancePrice() {
        console.log(this.data.balancePrice, 'balancePrice')
        if (this.data.balancePrice < 1) {
            app.toast('不可小于1')
            return
        }
        if (this.data.balancePrice == 0) {
            this.setData({
                balanceRemind: 0,
            })
            this.cancel()
            app.toast('检测到输入为‘0’自动关闭')
        }
        setBalanceRemind({
            balanceRemind: this.data.balancePrice
        }).then(() => {
            this.setData({
                balanceRemind: Number(this.data.balancePrice),
            })
            this.cancel()
            app.toast('修改成功', 'success')
        })
    },
    seteveryinsurancePrice() {
        if (this.data.everyinsurancePrice < 1) {
            app.toast('不可小于1')
            return
        }
        if (this.data.everyinsurancePrice == 0) {
            this.setData({
                everyinsuranceRemind: 0,
            })
            this.everyinsurancecancel()
            app.toast('检测到输入为‘0’自动关闭')
        }
        setFlagUserRemark({
            wesureReminderBalance: this.data.everyinsurancePrice,
            isWesureInsufficientBalanceReminder: 1
        }).then(() => {
            this.setData({
                everyinsuranceRemind: Number(this.data.everyinsurancePrice),
            })
            this.everyinsurancecancel()
            app.toast('修改成功', 'success')
        })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
        user.balanceRemind = this.data.balanceRemind
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})